// blog-common/src/main/java/paidaxing/blog/common/utils/MD5Util.java
package paidaxing.blog.common.utils;

import java.security.MessageDigest;

/**
 * MD5加密工具类
 */
public class MD5Util {

    private static final String SALT = "paidaxing_blog_2025"; // 加盐

    /**
     * MD5加密（加盐）
     */
    public static String encrypt(String password) {
        return md5(password + SALT);
    }

    /**
     * 验证密码
     */
    public static boolean verify(String password, String md5) {
        return md5.equals(encrypt(password));
    }

    /**
     * 原生MD5加密
     */
    private static String md5(String src) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] bytes = md.digest(src.getBytes("UTF-8"));
            return toHex(bytes);
        } catch (Exception e) {
            throw new RuntimeException("MD5加密错误", e);
        }
    }

    /**
     * 字节数组转十六进制
     */
    private static String toHex(byte[] bytes) {
        StringBuilder hex = new StringBuilder();
        for (byte b : bytes) {
            hex.append(String.format("%02x", b));
        }
        return hex.toString();
    }
}